var addLindID = "addMark";				//增加行按扭ID
var deleteLindID = "deleteAllMark";		//删除所有行按扭ID
var tableID = "tableMark";				//表格ID

var waterwaycode="";

$(function(){
	waterwaycode = getUrlParam("waterwaycode");
	
	
	$('#' + addLindID).click(function(){
		bindDetailList(null);
	});
	
	$('#' + deleteLindID).click(function(){
		$('#' + tableID).find('tbody').find('tr').not('.no-records-found').remove();
		
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
					    '   <td colspan="7" style="font-size:14px;">			'+
						'		单充底部“新增行”增加航标！		'+
						'	</td>						'+
						'</tr>';
			$('#' + tableID).find('tbody').append(trStr);
		}
	})
	
	getMarkInfo();
})


function getMarkInfo(){
	$.ajax({ 
		url: basePath() + 'waterway/getmark.do',
		data: {'waterwaycode': waterwaycode},
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
	    	if(data.result == 1){
    			for(var i=0; i<data.datas.length; i++){
    				bindDetailList(data.datas[i]);
    			}
	    	} 
      	}
	});
}

function bindDetailList(data){
	$('#' + tableID).find('.no-records-found').remove();
	
	var index = 1;
	if($('#' + tableID).find('tbody').find('tr').length > 0){
		index = parseInt($('#' + tableID).find('tbody').find('tr:last').find('.serialnumber').val()) + 1;
	}
	
    
    var navigationmarkcode = "";
    var navigationmarkname = "";
    var remark = "";
    
    if(data != null){
    	navigationmarkcode = data.navigationmarkcode;
    	navigationmarkname = data.navigationmarkname;
    	remark = data.remark;
    }
        	
	
	var trStr = '<tr id="tr_'+ index +'" class="data">'+
				'<form class="form-horizontal markForm" id="markForm_'+ index +'">'+
			    '    <td style="width:120px;">'+
			    '		<input type="hidden" name="waterwaycode" value="'+ waterwaycode +'">'+
			    '    	<input type="text" class="form-control" name="navigationmarkcode" value="'+ navigationmarkcode +'" placeholder="请填写航标编号" />'+
		        '	</td>'+
			    '    <td>'+
			    '    	<input type="text" class="form-control" name="navigationmarkname" value="'+ navigationmarkname +'" placeholder="请填写航标名称" />'+
			    '    </td>'+
			    '    <td>'+
			    '    	<input type="text" class="form-control" name="remark" value="'+ remark +'" />'+
			    '    </td>'+
			    '    <td>'+
    			'		<button type="button" class="Delete btn btn-danger btn-sm" onclick="deleteMarkLine(\'tr_'+ index +'\');" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span></button>'+
			    '    </td>'+
			    '</form>'+
			    '</tr>';
	
	$('#' + tableID).find('tbody').append(trStr);
}

function deleteMarkLine(trId){
	$('#' + trId).remove();
	
	if($('#' + tableID).find('tbody').children('tr').length == 0){
		var trStr = '<tr class="no-records-found">	'+
				    '   <td colspan="7" style="font-size:14px;">			'+
					'		单充底部“新增行”增加航标！		'+
					'	</td>						'+
					'</tr>';
		$('#' + tableID).find('tbody').append(trStr);
	}
}


function save() {
	var markFormData = $('#markForm').serializeObjectArray();

	if (!check(markFormData))
		return true;
		
	var param = {};
	param['mark'] = markFormData;
	
	$.ajax({
		method : 'post',
		url : projectPath + "waterway/savemark.do",
		data : JSON.stringify(param),
		dataType : "json",
		contentType : "application/json",
		success : function(data) {
	    	if(data.result == 1){
				window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.confirm,{
					onOk:function(v){
			    		parent.refreshData();		//刷新数据
					}
				});
	    	}
	    	else{
				window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.error);
	    	}
		}
	});
}


function check(data){	
	var result = true;
	

	if($('#tableMark').find('tbody').find('tr').not('.no-records-found').length == 0){
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>航标</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.info);
		
		return false;
	}
	
	
	var mustStr = "navigationmarkcode,navigationmarkname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var n = 0; n<data.length; n++){
		for(var i=0; i<mustStrs.length; i++){
			var obj = $('[name="'+mustStrs[i]+'"]');
			if(obj.val() == ""){
				strMsg += '<span style="color:red;">第' + (n+1) + '行</span>' + obj.attr("placeholder") + "！<br />";
				
				result = false;
			}
		}
	}
	

	
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.info);
	}
	
	return result;
}
